package org.terramagnet.mvcframework.http.signature.impl;

import org.terramagnet.mvcframework.http.Connection;
import java.util.Collection;
import java.util.TreeSet;

/**
 * 有序签名. 需要排序后再签名的可以继承本类.
 *
 * @author lip
 */
public abstract class SortedSignatureAlgorithm extends AbstractSecretSigner {

    @Override
    public String sign(Collection<Connection.KeyVal> parameters) {
        TreeSet<Connection.KeyVal> f;
        if (parameters instanceof TreeSet) {
            f = (TreeSet<Connection.KeyVal>) parameters;
        } else {
            f = new TreeSet<Connection.KeyVal>(parameters);
        }
        return signSortedFields(f);
    }

    /**
     * 生成签名字符串.
     *
     * <p>
     * <strong>签名操作是线程安全的。</strong></p>
     *
     * @param parameters 需要签名的有序字段
     */
    public abstract String signSortedFields(TreeSet<Connection.KeyVal> parameters);
}
